﻿using Microsoft.AspNetCore.Authorization;
using Simple.Common.Authorization;

namespace Microsoft.Extensions.DependencyInjection;

public static class AuthorizationServiceCollectionExtensions
{
    public static IServiceCollection AddSimpleAuthorization(this IServiceCollection services)
    {
        services.AddAuthorization(options =>
        {
            options.AddPolicy("EditMenuPolicy", policy =>
            {
                policy.RequireRole("test"); // 要求用户具有 "Admin" 角色
            });

            options.AddPolicy("DeleteMenuPolicy", policy =>
            {
                policy.RequireRole("test"); // 要求用户具有 "Admin" 角色
            });
        });

        services.AddTransient<IAuthorizationPolicyProvider, SimpleAuthorizationPolicyProvider>();
        services.AddTransient<IAuthorizationHandler, SimpleAuthorizationHandler>();
        services.AddTransient<IPermissionChecker, DefaultPermissionChecker>();

        return services;
    }
}
